package rmmk.algorithms.preprocessing.preprocessors;

import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import rmmk.framework.datasources.Document;

/**
 * Klasa użyta w DocumentReader do usuwania pustych dokumentów
 * 
 * @author Marcin
 * 
 */
public class RemoveEmptyDocuments {
	Logger logger = LoggerFactory.getLogger(RemoveEmptyDocuments.class);

	public List<Document> process(List<Document> documents) {
		int before = documents.size();
		/*int cores = Utils.getProcessorsCount();

		List<Document> syncDocs = Collections.synchronizedList(documents);

		Vector<Thread> threads = new Vector<>();
		for (int i = 0; i < cores; ++i) {
			Thread thread = new Thread(new Remover(syncDocs, cores, i));
			thread.start();
			threads.add(thread);
		}

		

		for (Thread t : threads)
			try {
				t.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
		
		/*for (int i = 0; i < documents.size(); ++i) {
			Document d = documents.get(i);

			if (d.getTEXT() == null || d.getTITLE() == null)
				documents.remove(i);
		}*/
		
		Iterator<Document> iter = documents.iterator();
		
		while(iter.hasNext()){
			Document d = iter.next();

			if (d.getTEXT() == null )//|| d.getTITLE() == null)
				iter.remove();
		}

		int after = documents.size();

		logger.info("Removed documents: " + (before - after));

		return documents;
	}

	public class Remover implements Runnable {

		int cores, shift;
		private List<Document> documents;

		public Remover(List<Document> documents, int cores, int shift) {
			this.documents = documents;
			this.cores = cores;
			this.shift = shift;
		}

		private int numberOfRemoved = 0;
		
		@Override
		public void run() {

			synchronized (documents) {
				/*for (int i = shift; i < documents.size(); i += cores) {
					Document d = documents.get(i);

					if (d.getTEXT() == null || d.getTITLE() == null)
						documents.remove(i);
				}*/
				
				Iterator<Document> i = documents.iterator();
				while(i.hasNext())
				{
					Document d = i.next();
					if (d.getTEXT() == null || d.getTITLE() == null){
						i.remove();
						++numberOfRemoved;
					}
				}
			}
			System.out.println("Remover ended, number of removed: " + this.numberOfRemoved);
		}
	}
}
